<template>
  <section class="component repair-info-upload">
    <el-form :model="modifyModel" :rules="rules" ref="modify-form" label-width="90px">
      <el-form-item label="上传附件" prop="fileIds">
        <file-upload :fileList.sync="fileList"></file-upload>
      </el-form-item>
      <el-form-item label="修复说明" prop="repairMemo">
        <el-input type="textarea" v-model="modifyModel.repairMemo" :maxlength="500"></el-input>
      </el-form-item>
    </el-form>
    <div class="operate-buttons">
      <el-button @click="emitClose">取消</el-button>
      <el-button @click="commit">提交</el-button>
    </div>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Dependencies } from "~/core/decorator";
import { State, Action, Getter } from "vuex-class";
import { PrincipalService } from "~/services/domain-service/principal.service";
import { Emit, Prop, Watch } from "vue-property-decorator";
import { Form } from "element-ui";
import { RepairCasesService } from "~/services/domain-service/repair-cases.service";
import FileUpload from "~/components/common/file-upload.vue"

@Component({
  components: {
    FileUpload
  }
})
export default class RepairInfoUpload extends Vue {
  @Dependencies(RepairCasesService) private repairCasesService: RepairCasesService;


  @Prop({ required: true })
  private id: string;


  private fileList: any[] = [];
  private modifyModel: any = {
    caseId: "",
    fileIds: [],
    repairMemo: ""
  }

  private rules = {
    fileIds: { required: true, message: "请上传附件" },
    repairMemo: { required: true, message: "请输入修复说明" }
  }


  @Emit("close")
  private emitClose() {
    const form = this.$refs['modify-form'] as Form
    form.resetFields()
    this.fileList = []
  }

  @Emit("success")
  private emitSuccess() {
    this.$message.success("操作成功")
    this.emitClose()
  }


  private commit() {
    this.modifyModel.caseId = this.id
    this.modifyModel.fileIds = this.fileList.map(v => v.response.id)
    const form = this.$refs['modify-form'] as Form
    form.validate(v => {
      if (!v) return
      this.repairCasesService.toRepair(this.modifyModel).subscribe(this.emitSuccess)
    })
  }


}
</script>

<style lang="less" scoped>
</style>
